// ChargeRecordDao.java 门诊收费数据访问类
package com.dw.dao.fee;


import com.dw.model.fee.ChargeRecord;
import com.dw.util.DBUtil;
import java.sql.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class ChargeRecordDao {
    public int createCharge(ChargeRecord record) throws SQLException {
        String sql = "INSERT INTO charge_record (charge_no, patient_id, doctor_id, amount, "
                + "charge_type, payment_method, status, remark) VALUES (?,?,?,?,?,?,?,?)";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {

            pstmt.setString(1, record.getChargeNo());
            pstmt.setInt(2, record.getPatientId());
            pstmt.setInt(3, record.getDoctorId());
            pstmt.setBigDecimal(4, record.getAmount());
            pstmt.setString(5, record.getChargeType());
            pstmt.setString(6, record.getPaymentMethod());
            pstmt.setString(7, record.getStatus());
            pstmt.setString(8, record.getRemark());

            pstmt.executeUpdate();
            try (ResultSet rs = pstmt.getGeneratedKeys()) {
                return rs.next() ? rs.getInt(1) : -1;
            }
        }
    }

    public ChargeRecord getByChargeNo(String chargeNo) throws SQLException {
        String sql = "SELECT * FROM charge_record WHERE charge_no = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, chargeNo);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    ChargeRecord cr = new ChargeRecord();
                    cr.setId(rs.getInt("id"));
                    cr.setChargeNo(rs.getString("charge_no"));
                    cr.setPatientId(rs.getInt("patient_id"));
                    cr.setDoctorId(rs.getInt("doctor_id"));
                    cr.setAmount(rs.getBigDecimal("amount"));
                    cr.setChargeType(rs.getString("charge_type"));
                    cr.setPaymentMethod(rs.getString("payment_method"));
                    cr.setChargeTime(rs.getTimestamp("charge_time"));
                    cr.setStatus(rs.getString("status"));
                    cr.setRemark(rs.getString("remark"));
                    return cr;
                }
            }
        }
        return null;
    }

    public int createWithConnection(Connection conn, ChargeRecord record) throws SQLException {
        String sql = "INSERT INTO charge_record (charge_no, patient_id, doctor_id, amount, charge_type, payment_method, status, remark, charge_time) "
                + "VALUES (?,?,?,?,?,?,?,?,?)";

        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, record.getChargeNo());
            pstmt.setInt(2, record.getPatientId());
            pstmt.setInt(3, record.getDoctorId());
            pstmt.setBigDecimal(4, record.getAmount());
            pstmt.setString(5, record.getChargeType());
            pstmt.setString(6, record.getPaymentMethod());
            pstmt.setString(7, record.getStatus());
            pstmt.setString(8, record.getRemark());
            pstmt.setTimestamp(9, Timestamp.valueOf(LocalDateTime.now())); // 添加时间戳

            return pstmt.executeUpdate();
        }
    }

    public List<String> getAllChargeNos() throws SQLException {
        List<String> chargeNos = new ArrayList<>();
        String sql = "SELECT charge_no FROM charge_record ORDER BY charge_time DESC";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql);
             ResultSet rs = pstmt.executeQuery()) {

            while (rs.next()) {
                chargeNos.add(rs.getString("charge_no"));
            }
        }
        return chargeNos;
    }

    public ChargeRecord getByChargeNoWithLock(Connection conn, String chargeNo) throws SQLException {
        String sql = "SELECT id, charge_no, patient_id, doctor_id, amount, charge_type, "
                + "payment_method, charge_time, remark, status "
                + "FROM charge_record WHERE charge_no = ? FOR UPDATE";

        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, chargeNo);

            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    ChargeRecord record = new ChargeRecord();
                    // 基础字段映射
                    record.setId(rs.getInt("id"));
                    record.setChargeNo(rs.getString("charge_no"));
                    record.setPatientId(rs.getInt("patient_id"));
                    record.setDoctorId(rs.getInt("doctor_id"));
                    record.setAmount(rs.getBigDecimal("amount"));
                    record.setChargeType(rs.getString("charge_type"));
                    record.setPaymentMethod(rs.getString("payment_method"));
                    record.setChargeTime(rs.getTimestamp("charge_time"));
                    record.setStatus(rs.getString("status"));  // 确保ChargeRecord类有status字段
                    record.setRemark(rs.getString("remark"));

                    return record;
                }
            }
        }
        return null;
    }

    public void updateStatus(Connection conn, String chargeNo, String status) throws SQLException {
        String sql = "UPDATE charge_record SET status = ? WHERE charge_no = ?";
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, status);
            pstmt.setString(2, chargeNo);
            pstmt.executeUpdate();
        }
    }
}